// pages/score/index.js


import { getScoreListRequest,
         getRowScoreListRequest } from '../../api/main'

const scoreCacheKey = "scores";
const rawScoreCacheKey = "rawScores";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 1,//1为原始成绩 0为有效成绩
    list:[],
    termIndex: 0,//当前学期索引
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getList()
  },

  getList(){
    const cache = wx.getStorageSync(this.data.type == 1 ? scoreCacheKey:rawScoreCacheKey)
    if(cache){
      this.setData({
        list:cache
      })
      return
    }else{
      this.update()
    }

  },
  update(){
    const that = this
    console.log(that.data.type+"test")
    that.data.type = Number(that.data.type)
    let p = null;
    if (that.data.type === 1){
      p = getScoreListRequest()
    }else{
      p = getRowScoreListRequest()
    }
    p.then(res=>{
      that.setData({
        list: res.data
      })
      wx.setStorageSync(that.data.type == 1 ? scoreCacheKey:rawScoreCacheKey, res.data)
    })
  },

  changeScoreType(e){
    const that = this
    const type = e.currentTarget.dataset.type
    that.setData({
      type
    })
    this.getList()
  },
  changeTerm(e){
    const termIndex = e.detail.value
    this.setData({
      termIndex
    })
  }


})